var session = require('../common/session')
var config = require('../common/config')

function isbind() {
  var openId = session.get("xyznzzopenId")
  var business = session.get('xyznzzbusiness')
  switch (business) {
    default:
      wx.reLaunch({
        url: 'pages/syslogin/syslogin'
      })
      break;
  }
  var model = {
    AuthProvider: 'miniprogram',
    ProviderKey: openId,
    ProviderAccessCode: '',
    ReturnUrl: '',
    SingleSignIn: true
  }
  //根据openId  获取 用户信息
  wx.request({
    url: config.service.host + '/api/TokenAuth/MiniProgramLogin',
    data: {
      AuthProvider: 'miniprogram',
      ProviderKey: openId,
      ProviderAccessCode: openId,
      ReturnUrl: '',
      SingleSignIn: true
    },
    method: 'POST',
    header: {
      'Content-Type': 'application/json' // 默认值
    },
    success: function (res) {
      if (res.data.success) {
        var data = res.data.result;
        if (data.userId === 0) {
          // 没有绑定用户
          wx.reLaunch({
            url: 'pages/syslogin/syslogin'
          })
        } else {
          // 绑定 用户 登录
          session.set('xyznzzaccessToken', data.accessToken);
        }
      } else {
        wx.reLaunch({
          url: 'pages/syslogin/syslogin'
        })
      }
    },
    fail: function (res) {
      console.log(res)
    }
  })

};

module.exports = {
  isbind
};